package com.example.task4.controller;
import com.example.task4.dao.CakeDao;
import com.example.task4.domain.Cake;
import com.example.task4.service.ShoppingCart;
import com.example.task4.service.ShoppingItem;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet(name = "cartServlet", value = "/cartServlet")
public class CartServlet extends HttpServlet {
    private String message;
    ShoppingCart cart;

    public void init() {
        message = "Hello World!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
        doPost(request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //request.setCharacterEncoding("UTF-8");
        String opt=request.getParameter("opt");
        System.out.println("opt:"+opt);
        if("show".equals(opt)){
            showCart(request,response);
        }
        if ("add".equals(opt)){
            addCart(request,response);
        }
        if ("remove".equals(opt)){
            removeCart(request,response);
        }
        if ("update".equals(opt)){
            updateQuantity(request,response);
        }
    }

    /**
 * 更新购物车中商品的数量
 * @param request
 * @param response
 * @throws IOException
 */
private void updateQuantity(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String id = request.getParameter("id");
    int num = Integer.parseInt(request.getParameter("num"));

    HttpSession session = request.getSession();
    ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");

    if (cart != null && cart.containsCake(id)) {
        cart.updateQuantity(id, num);
        System.out.println("成功更新商品 " + id + " 数量为：" + num);
    } else {
        System.out.println("购物车中未找到ID为 " + id + " 的商品");
    }

    response.sendRedirect("showcart.jsp");
}

    /**
     * 添加商品到购物车
     * @param request
     * @param response
     * @throws IOException
     */
    private void addCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取蛋糕信息
        String id=request.getParameter("id");
        CakeDao dao=new CakeDao();
        Cake cake=dao.findCakeById(id);
        int num=Integer.parseInt(request.getParameter("num"));
        System.out.println("Num:"+num);
        HttpSession session=request.getSession();

        if (session.getAttribute("cart")==null) {
            cart=new ShoppingCart();
        }
        else{
            cart=(ShoppingCart) session.getAttribute("cart");
        }
        cart.addItem(cake,num);
        session.setAttribute("cart",cart);
        System.out.println("成功添加购物车，购物车商品数量："+cart.getAmount());
        response.sendRedirect("index.jsp");
    }

    /**
     * 从购物车中移除商品
     * @param request
     * @param response
     * @throws IOException
     */
    private void removeCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        HttpSession session = request.getSession();
        ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");

        if (cart != null) {
            cart.removeItem(id);
        }

        response.sendRedirect("showcart.jsp");
    }

    private void showCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        cart=(ShoppingCart) request.getSession().getAttribute("cart");
        if(cart==null){
            System.out.println("购物车为空");
        }
        else{
            System.out.println("-----------购物车列表--------------");
            int num=1;
            for(ShoppingItem si:cart.getItems()){
                System.out.print("商品"+num++);
                System.out.print("\t名称："+si.getCake().getName());
                System.out.print("\t单价："+si.getCake().getPrice());
                System.out.println("\t小计："+si.getSubTotal());
            }
            System.out.println("---------------------------------");
            System.out.println("购物车商品总计："+cart.getTotalPrice());
        }
    }

    public void destroy() {
    }
}
